package com.olda.play.redisson;

import lombok.SneakyThrows;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.olda.play.constant.Constant.INITIALIZE_PROMPT;

@Configuration
public class RedissonConfig {
	@Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private String port;

    @Value("${spring.redis.password}")
    private String password;

    private static final Logger logger = LoggerFactory.getLogger(RedissonConfig.class);


    @SneakyThrows
    @Bean(destroyMethod = "shutdown")
    @ConditionalOnMissingBean
    public RedissonClient getRedisson(){

        Config config = new Config();
        if ("".equals(password.trim())) {
            password = null;
        }
        config.useSingleServer().setAddress("redis://" + host + ":" + port).setPassword(password).setPingConnectionInterval(1000);
        //添加主从配置
//        config.useMasterSlaveServers().setMasterAddress("").setPassword("").addSlaveAddress(new String[]{"",""});

        RedissonClient client = Redisson.create(config);
        logger.info(String.format(INITIALIZE_PROMPT,"redisson分布式锁client"));
        return client;
    }
}